Codeforces-976D Degree Set [思维]

原题链接

  codeforces 976D Degree Set

大致题意

  
给出一个长度为n的度数集合d(输入数据为一个递增数组d且无重复元素),要求你构造一个有d[n]+1个顶点的无向图,使得改无向图中的点的度数的集合与给出的集合相等

  
要求:

  1. 构造的无向图中包含d[n]+1个顶点
  2. 没有自环
  3. 没有重边
  4. 边数小于10^6
  5. 无向图的度数集与给出的度数集相等

题解

以下所说的图均为无向图

两个结论

  1. 对于一个顶点数为n的完全图,度数集合为{n-1}

    1
    2
    3
    4
    证明:
    n完全图中每个顶点都与另外n-1个点有连边
    即,n完全图中有n个度数为n-1的顶点
    度数集合为{n-1}
  2. 对于一个顶点数为n个顶点的图,取k∈[1,n],令[1,k]中所有顶点与图中所有顶点连边,则[1,k]中所有顶点度数为n-1,[k+1,n]中所有顶点度数为k。

    1
    2
    3
    4
    5
    6
    证明:
    1.对于顶点v∈[1,k]
    v与图中除自身外所有n-1个顶点都有连边,度数为n-1.
    2.对于顶点v∈[k+1,n]
    v与[1,k]中所有顶点连边,度数为k
    度数集合为{k,n-1}

其中结论1是结论2的特例(取k = n)

题目分析

  
我们现在d[n]+1个顶点,需要将其构造成一个度数集为{d[1],d[2]….d[n]的无向图}

  
考虑结论2,我们令[ 1,d[1] ]中所有顶点与图上所有顶点连边,则可得[ 1, d[1] ]中所有顶点度数为d[n],剩下所有顶点度数为d[1]。我们将图中顶点划分为3个部分

1
2
3
1. [ 1, d[1] ]中所有顶点度数为d[n]。
2. [ d[1]+1, d[n-1]+1 ]中所有顶点度数为d[1]。
3. [ d[n-1]+2, d[n] ]中所有顶点度数为d[1]。

  此时我们的度数集合为{d[1], d[n]}, 接下来只需要在第2部分中连边,使得度数集合中出现d[2],d[3]…d[n-1]即可。

  
问题转化为:构造一个顶点数为d[n-1]-d[1]+1的无向图,使其度数集为{d[2]-d[1],d[3]-d[1],….d[n-1]-d[1]},这是一个与原问题类似的规模更小的问题,可以重复上述操作。

核心代码

我们每次都将1~d[1]中顶点与图上所有顶点连边,然后忽略1~d[1]与d[n-1]+2~d[n]+1,在区间d[2]~d[n-1]+1上重复同样的操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//当前的最左端区间与最右端区间
int left = 1, right = n;
//记录边数
int cot = 0;
while (left <= right)
{
for (int i = d[left - 1] + 1; i <= d[left]; i++)
for (int j = i+1; j <= d[right] + 1; j++)
{
ans.push_back(edge(i,j));
cot++;
}
left++;
right--;
}

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <list>
#include <string.h>
using namespace std;

int d[310] = {};

struct edge
{
int u;
int v;
edge(int u, int v)
{
this->u = u;
this->v = v;
}
};
list<edge> ans;

int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> d[i];

int l = 1, r = n;
int cot = 0;
while (l <= r)
{
for (int i = d[l - 1] + 1; i <= d[l]; i++)
for (int j = i+1; j <= d[r] + 1; j++)
{
ans.push_back(edge(i,j));
cot++;
}
l++;
r--;
}

cout<<cot;
for(edge a : ans)
{
cout<<endl<<a.u<<' '<<a.v;
}
return 0;
}
  • © 2018-2020 宇亓
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

请我喝杯咖啡吧~

支付宝
微信